home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / rename < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  4.1 KB  |  156 lines

  1. #!/bin/ksh
  2. # @(#) rename.ksh 1.3 95/01/23
  3. # 90/06/01 John DuBois (spcecdt@armory.com)
  4. # 91/02/25 Improved help info
  5. # 92/06/07 remove quotes from around shell pattern as required by new ksh
  6. # 94/05/10 Exit if no globbing chars given.
  7. # 95/01/23 Allow file set to be given on command line.
  8.  
  9. lname=${0##*/}
  10. Usage="Usage: $lname [-htv] oldpattern newpattern [filename ...]"
  11. tell=false
  12. verbose=false
  13.  
  14. while getopts :htv opt; do
  15.     case $opt in
  16.     h)
  17.     echo \
  18. "$lname: rename files by changing parts of filenames that match a pattern.
  19. $Usage
  20. oldpattern and newpattern are subsets of sh filename patterns (the only
  21. globbing characters (wildcards) allowed are ? and *).  All files that match
  22. oldpattern will be renamed with the filename characters that match the constant
  23. (non-globbing) characters of oldpattern changed to the corresponding constant
  24. characters of newpattern.  The characters of the filename that match the
  25. globbing characters of oldpattern will be preserved.  Globbing characters in
  26. oldpattern must occur in the same order in newpattern; for every globbing
  27. character in newpattern there must be an identical globbing character in
  28. oldpattern in the same sequence.  Both arguments should be quoted since
  29. globbing characters are special to the shell.  If filenames are given, only the
  30. named files are acted on; if not, all files that match oldpattern are acted on.
  31. Examples:
  32. rename \"/tmp/foo*.ba.?\" \"/tmp/new*x?\"
  33. All files in /tmp that match foo*.ba.? will have the \"foo\" part replaced by
  34. \"new\" and the \".ba.\" part replaced by \"x\".  For example,
  35. /tmp/fooblah.ba.baz would be renamed to /tmp/newblahxbaz
  36. rename \* \*- foo bar baz
  37. foo, bar, and baz will be renamed to foo-, bar-, and baz-.
  38. Options:
  39. -h: Print this help.
  40. -v: Show the file move commands being executed.
  41. -t: Show what file moves would be done, but do not execute them."
  42.     exit 0
  43.     ;;
  44.     t)
  45.     tell=true
  46.     ;;
  47.     v)
  48.     verbose=true
  49.     ;;
  50.     +?)
  51.     print -u2 "$lname: options should not be preceded by a '+'."
  52.     exit 1
  53.     ;;
  54.     :) 
  55.     print -r -u2 -- \
  56.     "$lname: Option '$OPTARG' requires a value.  Use -h for help."
  57.     exit 1
  58.     ;;
  59.     ?) 
  60.     print -u2 "$lname: $OPTARG: bad option.  Use -h for help."
  61.     exit 1
  62.     ;;
  63.     esac
  64. done
  65.  
  66. # remove args that were options
  67. let OPTIND=OPTIND-1
  68. shift $OPTIND
  69.  
  70. oldpat=$1
  71. newpat=$2
  72.  
  73. case $# in
  74. [01])
  75.     print -u2 "$Usage\nUse -h for help."
  76.     exit 1
  77.     ;;
  78. 2)
  79.     set -- $1
  80.     if [[ ! -a $1 ]]; then
  81.     echo "No files match $oldpat."
  82.     exit
  83.     fi
  84.     ;;
  85. *)
  86.     shift 2
  87.     ;;
  88. esac
  89.  
  90. typeset -i i=1 j
  91.  
  92. # For old ksh
  93. # while [[ "$oldpat" = *'[\*\?]'* ]]; do
  94.  
  95. # Example oldpat: foo*.a
  96. # Example newpat: bar*.b
  97.  
  98. # Examples given for first iteration (in the example, the only interation)
  99. while [[ "$oldpat" = *[\*\?]* ]]; do
  100.     # Get leftmost globbing pattern in oldpat
  101.     pat=${oldpat#*[\*\?]}    # pat=.a
  102.     pat=${oldpat%%"$pat"}    # pat=foo*
  103.     pat=${pat##*[!\?\*]}    # pat=*
  104.     # Find parts before & after pattern
  105.     oldpre[i]=${oldpat%%"$pat"*}    # oldpre[1]=foo
  106.     oldsuf[i]=${oldpat#*"$pat"}        # oldsuf[1]=.a
  107.     newpre[i]=${newpat%%"$pat"*}    # newpre[1]=bar
  108.     # Get rid of processed part of patterns
  109.     oldpat=${oldpat#${oldpre[i]}"$pat"}    # oldpat=.a
  110.     newpat=${newpat#${newpre[i]}"$pat"}    # newpat=.b
  111.     let i=i+1
  112. done
  113.  
  114. if [ i -eq 1 ]; then
  115.     print -u2 "No globbing chars in pattern."
  116.     exit 1
  117. fi
  118.  
  119. oldpre[i]=${oldpat%%"$pat"*}    # oldpre[2]=.a
  120. oldsuf[i]=${oldpat#*"$pat"}    # oldsuf[2]=.a
  121. newpre[i]=${newpat%%"$pat"*}    # newpre[2]=.b
  122.  
  123. if $verbose; then
  124.     j=1
  125.     while [[ j -le i ]]; do
  126.     echo \
  127. "Old prefix: ${oldpre[j]}   Old suffix: ${oldsuf[j]}   New prefix: ${newpre[j]}"
  128.     let j=j+1
  129.     done
  130. fi
  131.  
  132. # Example file: foox.a
  133.  
  134. for file; do
  135.     j=1
  136.     origname=$file    # origname=foox.a
  137.     newfile=
  138.     while [[ j -le i ]]; do
  139.     # Peel off a prefix    interation    1        2
  140.     file=${file#${oldpre[j]}}        # file=x.a    file=
  141.     # Save the part of this prefix that is to be retained
  142.     const=${file%${oldsuf[j]}}        # const=x    const=
  143.     newfile=$newfile${newpre[j]}$const    # newfile=barx    newfile=barx.b
  144.     file=${file#$const}            # file=.a    file=.a
  145.     let j=j+1
  146.     done
  147.     if $tell; then
  148.     echo "Would move: $origname -> $newfile"
  149.     else
  150.     if $verbose; then
  151.         echo "Moving: $origname -> $newfile"
  152.     fi
  153.     mv $origname $newfile
  154.     fi
  155. done
  156.